﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using API.Models;
using PPG.DataAccess;
using PPG.CryptoProvider;
using System.Xml;
using System.Data;
using System.Configuration;
using System.Web;

namespace API.Controllers
{
    public class PatientDemographicsController : ApiController
    {
        /// <summary>
        /// Intializing Helper Classes - DB Connection and Security Provider for Encrypting and Decrypting Passwords
        /// Pass in DBServerName, DBUsern
        /// ame and DBPassword from Config File for DB connections
        /// The Security helper does nto take any parameters
        /// </summary>
        DataSourceCommunicator dsComm = new DataSourceCommunicator(DataSourceCommunicator.ParamType.ServerCredentials, ConfigurationManager.AppSettings["DBServerName"].ToString(), ConfigurationManager.AppSettings["DBUserName"].ToString(), ConfigurationManager.AppSettings["DBPassword"].ToString());
        
        [ActionName("PatientDemographics")]

        public AddReferralPatientDetailsResponse PostPatientDemographics([FromBody]AddReferralPatientDetailsRequest request)
        {
            AddReferralPatientDetailsResponse response = new AddReferralPatientDetailsResponse();

            dsComm.AddParameter("@ParamPatientID", request.PatientID);
            dsComm.AddParameter("@ParamFirstName",request.PatientFirstName);
            dsComm.AddParameter("@ParamLastName",request.PatientLastName );
            dsComm.AddParameter("@ParamDOB",request.DOB );
            dsComm.AddParameter("@ParamAddrLine1", request.AddrLine1);
            dsComm.AddParameter("@ParamAddrLine2", request.AddrLine2);
            dsComm.AddParameter("@ParamState", request.State);
            dsComm.AddParameter("@ParamZipCode", request.ZipCode);
            dsComm.AddParameter("@ParamPhoneNumber", request.PhoneNumber);
            dsComm.AddParameter("@ParamPhoneType", request.PhoneType);
            dsComm.AddParameter("@ParamEmail",request.Email );
            dsComm.AddParameter("@ParamPInsuranceDetl", request.Insurance );
            dsComm.AddParameter("@ParamPPlanNo", request.PlanNumber);
            dsComm.AddParameter("@ParamSInsuranceDetl", request.Insurance1);
            dsComm.AddParameter("@ParamSPlanNo", request.PlanNumber1);
            dsComm.AddParameter("@ParamPatientNotes",request.Notes );
            dsComm.AddParameter("@ParamReqPractice", request.RequestedPractice);
            dsComm.AddParameter("@ParamPhysicianID", request.PhysicianID);
            dsComm.AddParameter("@ParamPreferredPhysicianID", request.PreferredPhysician);
            dsComm.AddParameter("@ParamLoginID", request.LoginID);
            dsComm.AddParameter("@ParamRefReason", request.RefReason);
            dsComm.AddParameter("@ParamIsUrgentRequest", request.IsUrgentRequest);
            dsComm.AddParameter("@ParamPrefPhone", request.PrefPhone);
            dsComm.AddParameter("@ParamEstablishedPatient", request.EstablishedPatient);


            int i = int.Parse(dsComm.ExecuteScalar("dbo.spPostPatientDemographic").ToString());

            if (i > 0)
            {
                response.ErrorMessage = i.ToString() ;
                response.SaveStatus = "Saved";
            }
            else
            {
                //response.ErrorMessage = i.ToString();
                response.SaveStatus = "Not Saved";
            }

            return response;
        }


        public AddReferralPatientDetailsRequest GetPatientDemographics(int id)
        {
            AddReferralPatientDetailsRequest response = new AddReferralPatientDetailsRequest();

            dsComm.AddParameter("@ParamReferralID", id);
            
            string data = dsComm.ExecuteStoredProcedure("dbo.spGetPatientDemoForReferral");
            XmlTextReader xReader = new XmlTextReader(data, XmlNodeType.Document, null);
            DataSet PatientDataSet = new DataSet();
            PatientDataSet.ReadXml(xReader);

            if(PatientDataSet.Tables.Count > 0)
            {
                if(PatientDataSet.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = PatientDataSet.Tables[0].Rows[0];
                    response.PatientFirstName = dr["FirstName"].ToString();
                    response.PatientLastName = dr["LastName"].ToString();
                    response.DOB = DateTime.Parse(dr["DOB"].ToString());
                    response.AddrLine1 = dr["AddrLine1"].ToString();
                    response.AddrLine2 = dr["AddrLine2"].ToString();
                    response.State = dr["State"].ToString();
                    response.ZipCode = dr["ZipCode"].ToString();
                    response.PhoneNumber = dr["PhoneNumber"].ToString();
                    response.PhoneType = dr["PhoneType"].ToString();
                    response.Email = dr["Email"].ToString();
                    response.Insurance = dr["PrimaryInsurance"].ToString();
                    response.PlanNumber = dr["PrimaryPlanNumber"].ToString();
                    response.Insurance1 = dr["SecondaryInsurance"].ToString();
                    response.PlanNumber1 = dr["SecondaryPlanNumber"].ToString();
                    response.Notes = dr["PatientNotes"].ToString();
                    response.ReferralPhysician = dr["ReferringPhysician"].ToString();
                    response.PreferredPhysician = dr["PreferredPhysicianID"].ToString();
                    response.PhysicianID = dr["PhysicianID"].ToString();
                    response.PatientID = int.Parse(dr["PatientID"].ToString());
                    response.RequestedPractice = dr["RequestPracticeID"].ToString();
                    response.RefReason = dr["ReferralReason"].ToString();
                    response.IsUrgentRequest = bool.Parse(dr["IsUrgentRequest"].ToString());
                    response.PrefPhone = dr["PrefPhone"].ToString();
                    response.EstablishedPatient = bool.Parse(dr["EstablishedPatient"].ToString());
                    response.PPGPhysician = bool.Parse(dr["PPGPhysician"].ToString());
                }
            }

            return response;

        }
        /// <summary>
        /// Options Resposen for the preflight request of a CORS Opertaions. Will be standard across various functions
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage OptionsPatientDemographics()
        {
            var response = new HttpResponseMessage();
            response.StatusCode = HttpStatusCode.OK;
            return response;
        }

    }
}
